package com.yhy.wxzn.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.yhy.wxzn.common.enums.WcsTaskTypeEnums;
import com.yhy.wxzn.common.service.IWmsBusinessService;
import com.yhy.wxzn.wcs.device.rgv.enums.RgvProgress;
import com.yhy.wxzn.wcs.entity.TaskGenerateRequest;
import com.yhy.wxzn.wcs.entity.WcsFourWayTask;
import com.yhy.wxzn.wcs.enums.TaskStatus;
import com.yhy.wxzn.wcs.enums.TaskType;
import com.yhy.wxzn.wcs.mapper.WcsFourWayTaskMapper;
import com.yhy.wxzn.wcs.service.IWcsFourWayTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 四向车任务表
 * @Author: jeecg-boot
 * @Date:   2023-11-08
 * @Version: V1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WcsFourWayTaskServiceImpl extends ServiceImpl<WcsFourWayTaskMapper, WcsFourWayTask> implements IWcsFourWayTaskService {

    final WcsFourWayTaskMapper fourWayTaskMapper;
    @Autowired
    private IWmsBusinessService businessService;


    @Override
    public WcsFourWayTask getFirstCanExecuteTask(Integer id) {
        return fourWayTaskMapper.getFirstCanExecuteTask(id,TaskStatus.CANCEL.getTaskProgress(), TaskStatus.CANCEL.getTaskProgress(), RgvProgress.RGV_COMPLETE.getTaskProgress());
    }

    @Override
    public List<WcsFourWayTask> getUnAssignTask() {
        QueryWrapper<WcsFourWayTask> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(WcsFourWayTask::getAssignStatus,0)
                .ne(WcsFourWayTask::getTaskStatus, TaskStatus.FINISH.getTaskProgress())
        .ne(WcsFourWayTask::getRgvProgress,RgvProgress.RGV_COMPLETE.getTaskProgress());

        return list(wrapper);
    }



    @Override
    public void assignTask(Integer taskNo, Integer id) {
        fourWayTaskMapper.assignTask(taskNo,id);
    }

    @Override
    public boolean updateRgvProgress(WcsFourWayTask task, RgvProgress rgvProgress) {
        boolean b = fourWayTaskMapper.updateRgvProgress(task.getId(), rgvProgress.getTaskProgress());
        if (b){
            log.info("{}号小车更新任务进度为{}成功",task.getProcessRgvDeviceCode(),rgvProgress);
        }else {
            log.info("{}号小车更新任务进度为{}失败",task.getProcessRgvDeviceCode(),rgvProgress);
        }
        return b;
    }

    @Override
    public List<WcsFourWayTask> getUnfinishedByFourWayList() {
        LambdaQueryWrapper<WcsFourWayTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WcsFourWayTask::getTaskStatus, TaskStatus.READY.getTaskProgress())
                .in(WcsFourWayTask::getTaskType, Arrays.asList( WcsTaskTypeEnums.INBOUND_SXC.getValue(),WcsTaskTypeEnums.OUTBOUND_SXC.getValue(),WcsTaskTypeEnums.MOVE_SXC.getValue()))
                .orderByAsc(WcsFourWayTask::getCreateTime);
        return this.list(wrapper);
    }


    @Override
    public boolean updateTaskStatus(WcsFourWayTask task, com.yhy.wxzn.wcs.enums.TaskStatus taskStatus) {
        boolean b = fourWayTaskMapper.updateTaskStatus(task.getId(), taskStatus.getTaskProgress());
        if (b){
            log.info("{}号小车更新任务状态为{}成功",task.getProcessRgvDeviceCode(),taskStatus);
        }else {
            log.info("{}号小车更新任务状态为{}失败",task.getProcessRgvDeviceCode(),taskStatus);
        }
        return b;
    }

    @Override
    public void genFourWayTask(String source, String target, TaskType taskType) {
        LambdaQueryWrapper<WcsFourWayTask> taskQuery = new LambdaQueryWrapper<>();
        taskQuery.eq(WcsFourWayTask::getSource,source);
        taskQuery.eq(WcsFourWayTask::getTarget,target);
        taskQuery.eq(WcsFourWayTask::getTaskType,taskType);
        taskQuery.lt(WcsFourWayTask::getTaskStatus, com.yhy.wxzn.wcs.enums.TaskStatus.FINISH.getTaskProgress());
        WcsFourWayTask fourWayTask = this.getOne(taskQuery);
        if(ObjectUtils.isEmpty(fourWayTask)){
            fourWayTask = new WcsFourWayTask();
            fourWayTask.setRelateCode("");
            fourWayTask.setSource(source);
            fourWayTask.setTarget(target);
            fourWayTask.setTaskStatus(com.yhy.wxzn.wcs.enums.TaskStatus.READY);
            fourWayTask.setTaskType(taskType);
            fourWayTask.setPalletNo("");
            fourWayTask.setPriority(5);
            fourWayTask.setRgvProgress(RgvProgress.NOT_ASSIGNED_TO_RGV);
            this.save(fourWayTask);
        }
    }

    @Override
    public Result<?> generateTask(TaskGenerateRequest req) {
        WcsFourWayTask fourWayTask = null;

        switch (TaskType.parseTaskType(req.getTaskType())){
            case TRANSFER:
                fourWayTask = getTransferTask(req);
                break;
            case CHARGE:
                fourWayTask = getChangeTask(req);
                break;
            case MOVE_LOADED:
                fourWayTask = getMoveLoadedTask(req);
                break;
            case MOVE_UNLOADED:
                fourWayTask = getMoveUnloadedTask(req);
                break;
            default:
                return Result.error("暂不支持该任务类型");
        }


        if (fourWayTask == null){
            return Result.error("生成任务失败");
        }else {
            save(fourWayTask);
            return Result.OK("生成任务成功");
        }
    }

    private WcsFourWayTask getMoveUnloadedTask(TaskGenerateRequest req) {
        return WcsFourWayTask.buildManualTask(TaskType.MOVE_UNLOADED,req.getRgvId(),req.getStartNodeStr(),req.getEndNodeStr());
    }

    private WcsFourWayTask getMoveLoadedTask(TaskGenerateRequest req) {
        return WcsFourWayTask.buildManualTask(TaskType.MOVE_LOADED,req.getRgvId(),req.getStartNodeStr(),req.getEndNodeStr());
    }

    private WcsFourWayTask getChangeTask(TaskGenerateRequest req) {
        return WcsFourWayTask.buildManualTask(TaskType.CHARGE,req.getRgvId(),req.getStartNodeStr(),req.getEndNodeStr());
    }

    private WcsFourWayTask getTransferTask(TaskGenerateRequest req) {
        return WcsFourWayTask.buildManualTask(TaskType.TRANSFER,req.getRgvId(),req.getStartNodeStr(),req.getEndNodeStr());
    }
}
